<?php

/**
 * @author christian
 * @copyright 2011
 *  
 */


$ruta=$_SERVER['DOCUMENT_ROOT']."/viventura"."/MDOperator";
include_once("$ruta/star.php");




if (!isset($_POST['place_i'])) {$_POST['place_i'] = "NULL"; }
if (!isset($_POST['type'])) {$_POST['type'] = "NULL"; }
if (!isset($_POST['idTour'])) {$_POST['idTour'] = "NULL"; }


$place = $_POST['place_i'];
$idTour = $_POST['idTour'];
$idTourTrack = $_POST['type'];



    //// Al inicio del tour
    if($place==0){
        
        
        $ini = "select DateBegTrack from tour_has_track where TOUR_idTour=$idTour and OrdTrack=1";
        $ini_res = CModelo::getInstance()->getData($ini);
        
        foreach($ini_res as $r=>$c){
            $table = $c;
            foreach($table as $r2=>$c2){
                $NewEndTrack= $c2;
            }
        }
        
                
        $newTourTrack = "insert into tour_has_track (idTourTrack, OrdTrack, DateBegTrack, DateEndTrack, TOUR_idTour, TRACK_idTrack,RESTAURANT_idRest) VALUES 
                        (NULL,1,$NewBegTour,$NewEndTrack,$idTour,$idTrack,159)";
                        
        CModelo::getInstance()->insertData($newTourTrack);
      
        
        
        }
      
    elseif($place==1){
        
        $end = "select DateEndTrack, OrdTrack from tour_has_track where TOUR_idTour =$idTour and OrdTrack=(select count(*) from tour_has_track where TOUR_idTour=$idTour)";
        echo $end;
        $end_res = CModelo::getInstance()->getData($end);
        foreach($end_res as $r=>$c){
            $table = $c;
            foreach($table as $r2=>$c2){
                $DateOrd[]= $c2;
            }
        }
        
        //Lugar 1 de DateOrd es DateEndTrack del ultimo dia y lugar 2 es el OrdTrack de ese track;
        
         $newTourTrack = "insert into tour_has_track (idTourTrack, OrdTrack, DateBegTrack, DateEndTrack, TOUR_idTour, TRACK_idTrack,RESTAURANT_idRest) VALUES 
                        (NULL,$DateOrd[1]+1,(SELECT ADDDATE('$DateOrd[0]',1)),$NewEndTrack,$idTour,$idTrack,159)";
         echo $newTourTrack;
         CModelo::getInstance()->insertData($newTourTrack);
         
      
            
        }
    else{
        
        $end = "select DateEndTrack, OrdTrack from tour_has_track where idTourTrack =$idTourTrack";
        echo $end;
        $end_res = CModelo::getInstance()->getData($end);
        foreach($end_res as $r=>$c){
            $table = $c;
            foreach($table as $r2=>$c2){
                $DateOrd[]= $c2;
            }
        }
        
        echo $DateOrd[0];
        
        $newTourTrack = "insert into tour_has_track (idTourTrack, OrdTrack, DateBegTrack, DateEndTrack, TOUR_idTour, TRACK_idTrack,RESTAURANT_idRest) VALUES 
                        (NULL,$DateOrd[1]+1,(SELECT ADDDATE('$DateOrd[0]',1)),$NewEndTrack,$idTour,$idTrack,159)";
                        
                        
        $query = "SELECT DATEDIFF ($NewEndTrack,$DateOrd[0])";
        $diff = CModelo::getInstance()->getData($query);
      
        desplace($diff,$DateOrd[1]);
        
        }


/** escribiendo mi procedimiento almanceado
*/

    function desplace ($desp,$order,$tour){
        
         $actualizar = "UPDATE tour_has_track SET OrdTrack=OrdTrack+1 WHERE OrdTrack>=$order AND TOUR_idTour=$tour";
         
         $fechas = "UPDATE tour_has_track SET DateBegTrack = (SELECT ADDDATE('DateBegTrack',$desp)) WHERE OrdTrack>=$order AND TOUR_idTour=$tour";
                
    }


?>